from django.contrib.auth.models import AbstractUser
from django.db import models
from django.urls import reverse


class User(AbstractUser):
    login_count = models.IntegerField("登陆次数", default=0)
    last_ip = models.GenericIPAddressField("最后一次登陆的IP", blank=True, null=True)
    last_time = models.DateTimeField("最后一次登陆时间", blank=True, null=True)

    @property
    def full_name(self):
        full_name = '%s%s' % (self.last_name, self.first_name)
        return full_name.strip()

    full_name.fget.short_description = '全名'

    def __str__(self):
        return self.full_name or self.username

    class Meta:
        verbose_name = "用户"
        verbose_name_plural = verbose_name
        db_table = 'users'


class Company(models.Model):
    name = models.CharField("子公司名", max_length=256, unique=True)

    class Meta:
        verbose_name = "子公司"
        verbose_name_plural = verbose_name
        db_table = "company"

    def __str__(self):
        return self.name
